//1. 安装  npm i @reduxjs/toolkit
//2. 导入
import {createSlice, configureStore} from '@reduxjs/toolkit';

//3. 创建切片对象
let countSlice = createSlice({
  //设置状态的别名
  name: 'count',
  //初始值
  initialState: {value: 0},
  //配置 reducer
  reducers: {
    // 是 reducer 功能的一部分
    incre: (state, action) => {
      state.value += action.payload;
    },
    //
    decre: (state, action) => {
      state.value -= action.payload
    }
  }
});

//4. 获得 reducer 函数
let countReducer = countSlice.reducer;
let {incre, decre} = countSlice.actions;

//5. 配置 store 对象
let store = configureStore({
  reducer: countReducer
});


//分发任务
store.dispatch(incre(10)); //同步状态更新
store.dispatch(dispatch => {
  setTimeout(() => {
    dispatch(decre(2));
  }, 2000)
})
store.getState();